package ripple.common.entity;

import java.util.ArrayList;
import java.util.List;

public class Reform {

    public static int MAX_WAIT_TIME = 1000;

    public static String REFORM = "reform";
    public static String JOIN = "join";
    public static String LEAVE = "leave";

    public static List<NodeMetadata> merge(List<Reform> reformList) {
        return new ArrayList<>();
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    private String type;


    public List<NodeMetadata> getNodeMetadataList() {
        return nodeMetadataList;
    }

    public void setNodeMetadataList(List<NodeMetadata> nodeMetadataList) {
        this.nodeMetadataList = nodeMetadataList;
    }

    private List<NodeMetadata> nodeMetadataList;

    Reform(String type, List<NodeMetadata> nodelist) {
        this.setType(type);
        this.setNodeMetadataList(nodelist);
    }

    Reform(String type) {
        this.setType(type);
    }

    public static Reform reform() {
        return new Reform(REFORM);
    }

    @Override
    public String toString() {
        return this.getType() +
                "/" +
                NodeMetadata.ListToString(this.getNodeMetadataList());
    }

    public static Reform fromString(String s) {
        String[] info = s.split("/");
        return new Reform(info[0], NodeMetadata.ListFromString(info[1]));
    }

    public static String ListToString(List<Reform> reformList) {
        StringBuilder builder = new StringBuilder();
        for (Reform reform : reformList) {
            if (builder.length() != 0) {
                builder.append("|");
            }
            builder.append(reform.toString());
        }
        return builder.toString();
    }

    public static List<Reform> ListFromString(String s) {
        List<Reform> ret = new ArrayList<>();
        String[] nodeInfo = s.split("\\|");
        for(String info : nodeInfo) {
            ret.add(Reform.fromString(info));
        }
        return ret;
    }
}
